package com.teacold.customdemo.chapter_ten;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.teacold.customdemo.R;

public class MoveView extends SurfaceView {
    private Bitmap bgBitmap;
    private int mSurfaceWindth, mSurfaceHeight;
    private SurfaceHolder surfaceHolder;
    private boolean flag = false;//线程标示
    private int mBitposX;//开始绘制的图片的X坐标
    private Canvas mCanvas;
    private Thread thread;
    private Paint mPaint;

    //背景移动状态
    private enum State {
        LEFT, RINGHT;
    }

    //默认为想左
    private State state = State.LEFT;
    private final int BITMAP_STEP = 1;


    public MoveView(Context context) {
        this(context, null);
    }

    public MoveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint=new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(30);

        surfaceHolder = getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                flag = true;
//                startfaceAnimation();
                drawText();

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                flag = false;
            }
        });





    }
    private void drawText() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10 ; i++) {
                    Canvas canvas=surfaceHolder.lockCanvas();
                    if(canvas!=null){
                        canvas.drawText(i+"",i*30,50,mPaint);
                    }
                    surfaceHolder.unlockCanvasAndPost(canvas);
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    private void startfaceAnimation() {
        mSurfaceHeight = getHeight();
        mSurfaceWindth = getWidth();
        int mWindth = (int) (mSurfaceWindth * 3 / 2);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.timg);
        bgBitmap = Bitmap.createScaledBitmap(bitmap, mWindth, mSurfaceHeight, true);
        //开始绘图
        thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag){
                    mCanvas=surfaceHolder.lockCanvas();
                    DrawView();
                    surfaceHolder.unlockCanvasAndPost(mCanvas);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        thread.start();
    }







    private void DrawView() {
        //绘制背景
        mCanvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
        mCanvas.drawBitmap(bgBitmap,mBitposX,0,null);//绘制当前屏幕背景
        /*图片滚动效果*/
        switch (state){
            case LEFT:
                mBitposX-=BITMAP_STEP;
                break;
            case RINGHT:
                mBitposX+=BITMAP_STEP;
                break;
                default:
                    break;
        }
        if(mBitposX<=-mSurfaceWindth/2){
            state=State.RINGHT;
        }
        if(mBitposX>=0){
            state=State.LEFT;
        }

    }


}
